<?php

namespace App\Events;

use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\Models\Code;
use Curl\Curl;
use Illuminate\Support\Facades\Log;
/*use function Qiniu\json_decode;*/

class SendMsg extends Event implements ShouldBroadcast
{
    use SerializesModels;

    public $code;
    
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Code $code)
    {
        $this->code = $code;
        if($this->code->type == 99 ){
            $data = json_decode($this->code->content);
            $phone = $data->phone;

            $content = '【青苜网】您在青苜网发布的［'.$data->need.'］职位需求,收到新的面试邀请请登录青苜网进行查看';
        }   else {
            $content = $this->code->content;
            $phone =$this->code->phone;
        }

        $model = 0;
        switch($this->code->type){
            case 0:
                $model = 'SMS_155270299';
                break;
            default:
                $model = 'SMS_155270299';
                break;
        }

        include __DIR__.'/../Alidayu/TopSdk.php';
        date_default_timezone_set('Asia/Shanghai');

        $c = new \TopClient;
        $c ->appkey = "23652353" ;
        $c ->secretKey = "8256313fd3d68f3efaeb97acd58627c3";
        $req = new \AlibabaAliqinFcSmsNumSendRequest;
        $req ->setExtend( "" );
        $req ->setSmsType( "normal" );
        $req ->setSmsFreeSignName( "青苜网" );
        $req->setSmsParam("{\"cimo\":\"".$content."\"}");
        $req ->setRecNum($phone);
        $req ->setSmsTemplateCode($model);
        $resp = $c ->execute( $req );

        if($resp->result->success)
        {
            $this->code->update(['status'=>2]);

        }
        else
        {
            $this->code->update(['status'=>3]);
            Log::info(['短信发送失败'=>$resp]);
        }
        return ['code.'.$this->code->id];
    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        if($this->code->type == 99 ){
            $data = json_decode($this->code->content);
            $phone = $data->phone;

            $content = '【青苜网】您在青苜网发布的［'.$data->need.'］职位需求,收到新的面试邀请请登录青苜网进行查看';
        }   else {
            $content = $this->code->content;
            $phone =$this->code->phone;
        } 
        
        $model = 0;
        switch($this->code->type){
            case 0:
                $model = 'SMS_49125094';
                break;
            default:
                $model = 'SMS_49125094';
                break;
        }

        include __DIR__.'/../Alidayu/TopSdk.php';
        date_default_timezone_set('Asia/Shanghai');

        $c = new \TopClient;
        $c ->appkey = "23652353" ;
        $c ->secretKey = "8256313fd3d68f3efaeb97acd58627c3";
        $req = new \AlibabaAliqinFcSmsNumSendRequest;
        $req ->setExtend( "" );
        $req ->setSmsType( "normal" );
        $req ->setSmsFreeSignName( "青苜网" );
        $req->setSmsParam("{\"cimo\":\"".$content."\"}");
        $req ->setRecNum($phone);
        $req ->setSmsTemplateCode($model);
        $resp = $c ->execute( $req );

        if($resp->result->success)
        {
            $this->code->update(['status'=>2]);

        }
        else
        {
            $this->code->update(['status'=>3]);
            Log::info('短信发送失败'.$resp);
        }
        return ['code.'.$this->code->id];
    }


}
